package com.polaris.live.adapter

import com.blankj.utilcode.util.ColorUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.ext.formatNumberWithK
import com.polaris.live.databinding.AdapterRoomUserBinding
import com.polaris.live.resp.back_resp.LiveUserConsumptionVo
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * RoomUserAdapter
 *
 * @author Created by 半仙 on 2024/9/23/023 16:20
 */
class RoomUserAdapter(
    private val mHomeOwner: Long,
    private val mAdministrators: List<Long>?,
) : BaseBindingAdapter<AdapterRoomUserBinding, LiveUserConsumptionVo>() {

    init {
        addChildClickViewIds(R.id.cons_end, R.id.item_avatar)
    }

    override fun convert(
        holder: VBViewHolder<AdapterRoomUserBinding>,
        item: LiveUserConsumptionVo,
    ) {
        holder.vb.apply {
            val self = item.base
            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(self.avatarFrame))
            itemAvatar.setImageUrl(self.avatar, self.userId)
            if (item.background != null) {
                Glide.with(bgImg)
                    .load(ImageUtils.imgPath(item.background))
                    .into(bgImg)
                tvCoin.setTextColor(ColorUtils.getColor(R.color.white))
            } else {
                tvCoin.setTextColor(ColorUtils.getColor(R.color.white))
                bgImg.setImageResource(0)
            }

            itemName.text = self.nickname
            itemName.setNicknameColor(self.nicknameColor, color.white)
            country.setCountry(self.country)
            ageSex.setSexAndAge(self.sex, self.birthday)
            vipView.setVipLevel(self.vipLevel)

            if (CommonUtils.isMan(self.sex)) {
                level1.setRichsLevel(self.richs)
                level2.setCharmsLevel(self.charms)
            } else {
                level1.setCharmsLevel(self.charms)
                level2.setRichsLevel(self.richs)
            }


            tvCoin.text = item.coin.formatNumberWithK()

            llFlag.setLabel(item.base.label)

            if (mHomeOwner == self.userId) {
                ivUserIdentity.setImageResource(R.drawable.ic_rank_room)
            } else {
                ivUserIdentity.setImageResource(R.drawable.ic_rank_indentity_audience)
                if (mAdministrators?.isNotEmpty() == true && item.base.userId in mAdministrators) {
                    ivUserIdentity.setImageResource(R.drawable.ic_rank_indentity_administrator)
                }
            }
        }
    }
}